import api from '../../api';
import { message } from 'antd';
import { useCallback } from 'react';
export default {
  namespace: 'limitAlarmqueryM',

  state: {
    selectData: [],
    tableDataObj: {}, //列表数据
  },

  effects: {
    //获取限额信息下拉
    *getSelectData({ payload }, { call, put }) {
      const response = yield call(api.limitAlarmquery.getSelectData, payload);
      if (response && response.success == true) {
        const arr = [];
        if (response.data.data) {
          response.data.data.map((item) => {
            arr.push({
              title: item.ruleDesc,
              value: item.ruleId,
            });
          });
        }

        yield put({
          type: 'save',
          payload: {
            selectData: arr,
          },
        });
      } else {
        message.error('限额信息下拉列表查询失败!');
      }
    },
    // 获取表格数据
    *queryWaringList({ payload, callback }, { call, put, select }) {
      const response = yield call(api.limitAlarmquery.queryWaringList, payload);
      if (response) {
        if (response.code == 0) {
          yield put({
            type: 'save',
            payload: {
              tableDataObj: response.data || {},
            },
          });
        } else {
          yield put({
            type: 'save',
            payload: {
              tableDataObj: {
                columns: [],
                dataSource: [],
              },
            },
          });
          message.error(response.msg || '限额触警列表查询失败!');
        }
      } else {
        yield put({
          type: 'save',
          payload: {
            tableDataObj: {
              columns: [],
              dataSource: [],
            },
          },
        });
        message.error('限额触警列表查询失败!');
      }
    },
    //获取详情页数据
    *getDetails({ payload, callback }, { call, put }) {
      const response = yield call(api.limitAlarmquery.getWariningDetail, payload);
      if (response && response.code == 0) {
        callback(response);
      } else {
        callback({});
        message.error('查询失败');
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
